<template>
  <!--为什么用template，原因是vue编译template是不会显示的，
  这样的话就不会影响布局-->
  <template v-if="isshow">
    <slot></slot>
  </template>
</template>

<script>
// 注意千万不要在缓存里面去缓存
// import store from '@/store/index';
import {mapGetters} from 'vuex'
// import userService from '@/services/user/index'
import {decryptByDES} from "@/utils/utils";
import router from "@/router";

export default {
  name: "PugPermission",
  props: {
    fields: {
      type: Array,
      default: []
    },
    code: {
      type: String,
      default: ""
    },
    field: {
      type: String,
      default: ""
    },
    path: {
      type: String,
      default: ""
    },
  },

  computed: {
    ...mapGetters('login', ["permissons"]),
    isshow() {
      // 这里是从状态管理获取
      var permissionsList = this.permissons;
      var permissionObj = {};
      var permissionObj2 = {};
      permissionsList.forEach(obj => {
        permissionObj[decryptByDES(obj.code)] = obj;
        permissionObj2[decryptByDES(obj.url)] = obj;
      })

      // 只能判断首页
      if (!permissionObj2['/index']) {
        router.push("/500");
        return;
      }

      if (this.code) {
        return permissionObj[this.code] ? true : false;
      }

      if (this.path) {
        return permissionObj2[this.path] == undefined ? false : true;
      }

      if (this.field) {
        return this.fields.findIndex(f=>f == this.field) != -1;
      }

      return false;
    }
  }

  // data() {
  //   return {
  //     isshow: true,
  //     permissionsList:[]
  //   }
  // },
  //
  // created() {
  //     userService.permissions().then(res => {
  //       var permissionObj = {};
  //       this.permissionsList = res.data;
  //       this.permissionsList.forEach(obj => {
  //         permissionObj[decryptByDES(obj.code)] = obj;
  //       })
  //       this.isshow = permissionObj[this.code];
  //     })
  // }
}
</script>
